<?php

namespace App\Models;

use App\Models\CommonModel as Model;

/**
 * Class meeting
 * @package App\Models
 * @version July 14, 2018, 15. 26
 *
 * @property string name
 */
class Meeting extends Model
{
    const UNIT = '人民币/人';
    const CATEGOEY_MAP = [
        'A类' => '2000',
        'B类' => '2900',
        'C类' => '3700',
    ];

    const CATEGOEY_MAP_2 = [
        'A类' => '(包含资料、餐饮)',
        'B类' => '(含三晚双人间一个床位,合住; 资料、餐饮)',
        'C类' => '(含三晚单人房或双人房一间,合住; 资料、餐饮)',
    ];

    public $table = 'meetings';

    public $fillable = [
        'name',
        'cover',
        'start_time',
        'end_time',
        'tel',
        'english_name',
        'owner',
        'owner2',
        'theme',
        'address',
        'city_id',
        'city_name',
        'display_order',
        'icon',
        'arrangement',
        'corps',
        'guests',
        'supporting',
        'category',
        'guide_uri',
        'category',
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'name' => 'string',
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'name' => 'true'
    ];

    /**
     * @param $searchOptions
     * @return mixed
     */
    public  function getQueryBySearchOptions($searchOptions)
    {
        $qb = self::newQuery();
        $qb->select(['id', 'name', 'created_at', 'start_time', 'end_time', 'tel', 'theme', 'owner', 'city_id', 'address'
        , 'english_name', 'icon', 'city_name', 'display_order', 'title', 'owner2', 'post_uri', 'guide_uri', 'company_uri', 'category']);

        if (empty($searchOptions)) {
            return $qb;
        }

        !empty($searchOptions['keywords']) && $qb = $qb->where('keywords', 'like', '%' . $searchOptions['keywords']. '%');
        unset($searchOptions['keywords']);

        foreach ($searchOptions as $key => $value) {
            if (is_array($value)) {
                $qb->whereIn($key, $value);
            } elseif (is_string($value) || is_numeric($value)) {
                $qb->where($key, $value);
            }
        }

        return $qb;
    }
}
